#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# 거래통합로그 수집 데몬에 테스트 전문 전송하는 클라이언트
# Usage : ./total_log_send_clinet.py -f test_data -n 2

import sys
import string
import socket
import time

HOST = "127.0.0.1"
PORT = "30011"

def usage():
  print """
Usage
====
python %s -f FILENAME -n CNT : sock_clnt sends CNT times from FILENAME
""" % (sys.argv[0], sys.argv[0])

if not sys.argv[1:] or sys.argv[1] not in ['-f'] or sys.argv[3] not in ['-n']:
  usage()
else:
  try:
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  except socket.error, e:
    sys.stderr.write("[Socket ERROR] %s\n" % e[1])
    sys.exit(1)

  try:
    f = open(sys.argv[2])
    line = f.readline()

    for i in range(0, string.atoi(sys.argv[4])):
      sock.sendto(line, (HOST, string.atoi(PORT)))
      print i, "번째 전송"
      print line
      time.sleep(1)

    print "End of File"
    sock.close()

  except IOError:
    print "FILE(%s) does not exist!" % sys.argv[2]
    sock.close()

  sys.exit(0)
